import { useState, useEffect } from 'react';
import { Form, Switch, Input, message } from 'antd';
import CommonLoading from '@/components/CommonLoading';
import CommonModal from '@/components/CommonModal';
import { getCurrentUser } from '@/utils/authority';
import { getVipPriceConfigOp, setVipPriceConfigOp } from '../service';
import styles from '../index.module.less'

let source = {};

export default ({ isCustom, visible, handleCancel }) => {

  const [form] = Form.useForm();
  const [loading, setLoading] = useState(false)
  const [isOpen, setIsOpen] = useState(false)

  useEffect(()=>{
    if(visible){
      getConfigData()
    }
  },[visible])

  useEffect(()=>{
    form.resetFields();
  },[source])

  //获取配置信息
  const getConfigData = async () =>{
    setLoading(true);
    const { success, data = {} } = await getVipPriceConfigOp({
      uuid:getCurrentUser().uuid
    });
    if(success){
      source = data;
      const { status, customStatus } = data;
      setIsOpen(isCustom ? customStatus : status);
    }else{
      source = {};
    }
    setLoading(false);
  }

  //更新配置
  const onOk =  () => {
    form.validateFields().then(async values=>{
      setLoading(true)
      const { success, msg = '操作成功' } = await setVipPriceConfigOp({
         ...values,
         [isCustom ? 'customStatus' : 'status']: isOpen ? 1 : 0,
         id: source.id
      });
    if(success){
      message.success(msg)
      handleCancel()
    }
    setLoading(false);
    })
  
  };

  //弹窗取消操作
  const onCancel = () =>{
    source = {}
    setIsOpen(false)
    form.resetFields()
    handleCancel()
  }

  return (
    <>
    <CommonModal
      title="改价设置"
      visible={visible}
      onCancel={onCancel}
      onOk={onOk}
      width="600px"
    >
      <div className={styles.tipBoxCopy}>
          <p>①. 一周内商品改价金额高于原来金额次数超出设置的次数时需要审核是否生成 新的会员价 </p>
          <p>②. 改价金额与原来拿货金额的差额每超出设置的金额，需要用户审核是否改</p>
      </div>
      <Form layout={'inline'} form={form} initialValues={{...source}} style={{marginBottom:16}} name='changePriceConfig'>

        {
          isCustom ? <>
            <Form.Item label="改价次数" name="customChangeTimes" rules={[{ required: true }, {pattern:/^([1-9]\d*)$/, message: '请输入大于等于1的整数'}]}>
              <Input />
            </Form.Item>
            <Form.Item label="超出金额" name="customFloatingAmount" rules={[{ required: true }, {pattern:/^[1-9]\d*(\.\d{1,2})?$/, message: '大于等于1, 最多两位小数'}]}>
              <Input />
            </Form.Item>
          </> : <>
            <Form.Item label="改价次数" name="changeTimes" rules={[{ required: true }, {pattern:/^([1-9]\d*|[0]{1,1})$/, message: '请输入大于等于0的整数'}]}>
                <Input />
            </Form.Item>
            <Form.Item label="超出金额" name="floatingAmount" rules={[{ required: true }, {pattern:/^\d+(\.\d{1,2})?$/, message: '大于等于0, 最多两位小数'}]}>
              <Input />
            </Form.Item>
          </>
        }

      </Form>
      是否开启设置：<Switch checkedChildren="已开启" unCheckedChildren="已关闭"  checked={isOpen} style={{ margin:'6px' }} onClick={(e)=>setIsOpen(e)} size='default'/>
    </CommonModal>
   <CommonLoading loading={loading} />
  </>
  );
};
